package com.lanou.activity.controller;

import com.github.pagehelper.PageInfo;
import com.lanou.activity.Service.PlainInfoService;
import com.lanou.activity.utils.ToolUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/plain")
public class PlainInfoController {

    private Logger logger = LoggerFactory.getLogger(PlainInfoController.class);

    @Resource
    private PlainInfoService plainInfoService;

    /**
     * @param
     * @return
     * @description 跳转到添加企划信息页面
     * @author 66
     * @date 17/9/22
     */
    @RequestMapping("/plain")
    public ModelAndView addPlain(){
        ModelAndView modelAndView = new ModelAndView();
//        查询所有的商品类型
        List<Map<String, Object>> list= ( List<Map<String, Object>>) ToolUtil.callUrl("http://10.90.86.249:8888/prd/getTypeList");
        modelAndView.addObject("prodlist", list);
//        查询所有促销条件的信息
        List<Map<String, Object>> selectsalelist = plainInfoService.selectsale();
        modelAndView.addObject("selectsalelist", selectsalelist);
//        查询所有优惠的信息
        List<Map<String, Object>> selectdislist = plainInfoService.selectdis();
        modelAndView.addObject("selectdislist", selectdislist);
//        查询所有限额条件的信息
        List<Map<String, Object>> selectdlmtlist = plainInfoService.selectlimit();
        modelAndView.addObject("selectdlmtlist", selectdlmtlist);
        modelAndView.setViewName("addPlainInfo");
        return modelAndView;
    }


    /**
     * @description  企划管理查询并分页
     * @author 书豪
     * @date 17/9/26
     * @param
     */
    @RequestMapping("/plainInfo")
    public ModelAndView toPlainInfo(@RequestParam Map<String,Object> param,HttpSession session){
        System.out.println("=========================================");
        logger.info("sessionId = " + session.getId());
        System.out.println("=========================================");
        ModelAndView modelAndView = new ModelAndView("plainInfo");
        //所有的优惠券信息
        Map<String, Object> map = (Map<String, Object>) ToolUtil.callUrl("http://10.90.86.250:8070/rest/cpnPage.do");
        Map data = (Map) map.get("data");
        List cpnList = (List) data.get("listcpn");
        //查询所有促销条件的信息
        List<Map<String, Object>> saleList = plainInfoService.selectsale();

        //根据条件查询数据库并分页
        PageInfo pageInfo = plainInfoService.selectPlain(param);


        //将数据全部添加到 modelAndView 中
        modelAndView.addObject("page",pageInfo);
        modelAndView.addObject("list",pageInfo.getList());
        modelAndView.addObject("saleList",saleList);
        modelAndView.addObject("cpnList",cpnList);
        modelAndView.addObject("param",param);
        return modelAndView;
    }


    /**
     * @param
     * @description 根据对应的商品类别编号查询优惠券信息
     * @author 66
     * @date 17/9/25
     */
    @RequestMapping("cpn")
    @ResponseBody
     public  List<Map<String, Object>> selectcpn(@RequestParam("prdNo") String prd_no) {
        Map map = new HashMap();
        map.put("cpnTypeNo", prd_no);
        Map<String, Object> map1 = (Map<String, Object>) ToolUtil.callUrl("http://10.90.86.250:8070/rest/cpnPage.do", map);
        Map<String, Object> map2 = (Map<String, Object>) map1.get("data");
        List<Map<String, Object>> list = (List<Map<String, Object>>) map2.get("listcpn");

        return list;
    }

    /**
     * @param
     * @description 添加企划信息
     * @author 66
     * @date 17/9/25
     */
    @RequestMapping("addPlain")
    @ResponseBody
    public  Map<String, Object>  addPlainInfo(@RequestParam Map<String, Object> params) {
        System.out.println(params.toString());
        String value=ToolUtil.AccountGenerator("sp");
        params.put("spNo",value);
        String value2=ToolUtil.dataToolDate();
        params.put("instDate",value2);
//        添加企划信息
        plainInfoService.addPlain(params);
         params.put("result","success");
        return params;
    }

    /**
     * @description 转发到企划管理页面并回显数据
     * @author 书豪
     * @date 17/9/26
     * @param
     */
    @RequestMapping("findPlainInfoDetail")
    public ModelAndView findPlainInfoDetail(@RequestParam Map<String,Object> param, HttpSession session){
        System.out.println("=========================================");
        logger.info("sessionId = " + session.getId());
        System.out.println("=========================================");
        ModelAndView modelAndView = new ModelAndView("plainDetail");
        //查询所有促销条件的信息
        List<Map<String, Object>> saleList = plainInfoService.selectsale();
        //当前id的详细信息
        List list =plainInfoService.selectPlainById(param);
        //所有的代金券券信息
        Map<String, Object> map = (Map<String, Object>) ToolUtil.callUrl("http://10.90.86.250:8070/rest/cpnPage.do");
        Map data = (Map) map.get("data");
        List cpnList = (List) data.get("listcpn");
        //查询所有限额条件的信息
        List<Map<String, Object>> selectdlmtlist = plainInfoService.selectlimit();
        //查询所有优惠的信息
        List<Map<String, Object>> selectdislist = plainInfoService.selectdis();

        //将数据添加至 modelAndView 中
        modelAndView.addObject("selectdislist",selectdislist);
        logger.info("优惠的信息  :"+selectdislist);
        modelAndView.addObject("selectdlmtlist", selectdlmtlist);
        logger.info("限额条件的信息  :"+selectdlmtlist);
        modelAndView.addObject("plainInfo",list.get(0));
        logger.info("当前企划的具体信息   :"+list.get(0));
        modelAndView.addObject("saleList",saleList);
        logger.info("促销的信息  :"+saleList);
        modelAndView.addObject("cpnList",cpnList);
        logger.info("代金券的信息"+cpnList);
        return modelAndView;
    }


    /**
     * @description  企划管理更新
     * @author 书豪
     * @date 17/9/26
     * @param
     */
    @RequestMapping("updatePlainInfo")
    @ResponseBody
    public Map<String,Object> updatePlainInfo(@RequestParam Map<String, Object> params){
        logger.info(params.toString());
        plainInfoService.updatePlain(params);
        params.put("result","success");
        return params;
    }

}
